package com.easy.core;

import java.util.List;
import java.util.Properties;

import org.h2.util.StringUtils;
import org.mybatis.guice.XMLMyBatisModule;

import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class Context {
	private static Injector injector;

	private static String jdbcUrl = getJdbcUrl();
	static {
	    Properties prop = new Properties();
	    prop.setProperty("jdbc.driver", "org.h2.Driver");
	    prop.setProperty("jdbc.url", jdbcUrl);
	    prop.setProperty("jdbc.username", "root");
	    prop.setProperty("jdbc.password", "root");
	    
		List<Module> modules = Lists.newArrayList();
		modules.add(new XMLMyBatisModule() {
			@Override
			protected void initialize() {
			    addProperties(prop);
				setEnvironmentId("easy");
				setClassPathResource("sqlMapConfig.xml");
			}
		});
		injector = Guice.createInjector(modules);
	}
	public static Injector getInjector() {
		return injector;
	}
	
	public static String getJdbcUrl() {
	    String dir = System.getProperty("user.dir");
        System.out.println("user.dir"+dir);
        dir = StringUtils.replaceAll(dir, "\\", "/");
        System.out.println("user.dir"+dir);
        String url = "jdbc:h2:tcp://localhost:9092/" + dir +"/h2db/easycms;MODE=MySQL";
        return url;
	}
}
